import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/model/general_chart_model.dart';
import 'package:x3_charts_core/touch_fnfo/generic_stacked_chart_touch_info.dart';
import 'package:x3_charts_core/chart_painter/generic_stacked_chart_painter.dart';

///**
///* @author hrongyong
///* @date   1/26/21
///* @email hrongyong@gmail.com
///* Description: 通用层叠图表
///*/

class GenericStackedChartWidget extends StatefulWidget {
  final List<GeneralChartModel> chartList;

  ///背景颜色
  final Color bgColor;

  ///底坐标颜色
  final Color bottomCoordinateColor;

  ///是否要绘画x轴name上面的刻度线
  final bool drawXTickMark;

  ///第一个直方图的颜色
  final Color firstHistogramColor;

  ///第二个直方图的颜色
  final Color secondHistogramColor;

  ///第三个直方图的颜色
  final Color thirdHistogramColor;

  ///第一个折线的颜色
  final Color firstPolylineColor;

  ///第二个折线的颜色
  final Color secondPolylineColor;

  ///是否要显示数据
  final bool isDisplayData;

  ///直方图的点击事件
  final Function(int) clickEventOfHistogram;

  ///第一个直方图的点击事件
  final Function(int) clickEventOfTheFirstHistogram;

  ///第二个直方图的点击事件
  final Function(int) clickEventOfTheSecondHistogram;

  ///第三个直方图的点击事件
  final Function(int) clickEventOfTheThirdHistogram;

  ///第一个折线图的点击事件
  final Function(int) clickEventOfTheFirstLineChart;

  ///第一个折线图的点击事件
  final Function(int) clickEventOfTheSecondLineChart;

  ///异常颜色-目前只用于直方图
  final Color abnormalColor;

  ///左边Y轴最大值
  final int leftYAxisMax;

  ///左边Y轴的单位
  final String leftYAxisUnit;

  ///右边Y轴最大值
  final int rightYAxisMax;

  ///右边Y轴的单位
  final String rightYAxisUnit;

  GenericStackedChartWidget({
    Key key,
    @required this.chartList,
    this.firstHistogramColor = Colors.blue,
    this.secondHistogramColor = Colors.lightBlue,
    this.thirdHistogramColor = Colors.lightBlueAccent,
    this.firstPolylineColor = Colors.orange,
    this.secondPolylineColor = Colors.orangeAccent,
    this.bgColor = Colors.white,
    this.bottomCoordinateColor = Colors.white,
    this.abnormalColor = const Color.fromARGB(255, 194, 194, 194),
    this.drawXTickMark = false,
    this.isDisplayData = false,
    this.clickEventOfHistogram,
    this.clickEventOfTheFirstHistogram,
    this.clickEventOfTheSecondHistogram,
    this.clickEventOfTheThirdHistogram,
    this.clickEventOfTheFirstLineChart,
    this.clickEventOfTheSecondLineChart,
    this.leftYAxisMax,
    this.leftYAxisUnit = "",
    this.rightYAxisUnit = "",
    this.rightYAxisMax,
  });

  @override
  _GenericStackedChartWidgetState createState() => _GenericStackedChartWidgetState();
}

class _GenericStackedChartWidgetState extends State<GenericStackedChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  final GenericStackedChartTouchInfo touchInfo = GenericStackedChartTouchInfo();

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    touchInfo.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return GestureDetector(
            onTapUp: (TapUpDetails detail) {
              ///点击直方
              if (widget.clickEventOfHistogram != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheHistogram(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfHistogram(_index);
              }

              ///点击第一个直方
              if (widget.clickEventOfTheFirstHistogram != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheFirstHistogram(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfTheFirstHistogram(_index);
              }

              ///点击第二个直方
              if (widget.clickEventOfTheSecondHistogram != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheSecondHistogram(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfTheSecondHistogram(_index);
              }

              ///点击第三个直方
              if (widget.clickEventOfTheThirdHistogram != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheThirdHistogram(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfTheThirdHistogram(_index);
              }

              ///点击第一个折线
              if (widget.clickEventOfTheFirstLineChart != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheFirstPolyline(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfTheFirstLineChart(_index);
              }

              ///点击第二个折线
              if (widget.clickEventOfTheSecondLineChart != null && _controller.value == 1.0) {
                int _index = touchInfo.clickOnTheSecondPolyline(Offset(detail.localPosition.dx, detail.localPosition.dy - constraints.maxHeight));
                widget.clickEventOfTheSecondLineChart(_index);
              }
            },
            child: CustomPaint(
              size: Size(constraints.maxWidth, constraints.maxHeight),
              painter: GenericStackedChartPainter(
                  leftYAxisMax: widget.leftYAxisMax,
                  leftYAxisUnit: widget.leftYAxisUnit,
                  rightYAxisMax: widget.rightYAxisMax,
                  rightYAxisUnit: widget.rightYAxisUnit,
                  chartList: widget.chartList,
                  touchInfo: touchInfo,
                  bgColor: widget.bgColor,
                  drawXTickMark: widget.drawXTickMark,
                  firstHistogramColor: widget.firstHistogramColor,
                  secondHistogramColor: widget.secondHistogramColor,
                  thirdHistogramColor: widget.thirdHistogramColor,
                  firstPolylineColor: widget.firstPolylineColor,
                  secondPolylineColor: widget.secondPolylineColor,
                  repaint: _controller,
                  isDisplayData: widget.isDisplayData,
                  abnormalColor: widget.abnormalColor,
                  bottomCoordinateColor: widget.bottomCoordinateColor),
            ),
          );
        },
      ),
    );
  }
}
